package cc.rengu.igas.channel.esb.core.service.trans.corebank;

import cc.rengu.igas.channel.esb.common.constant.EsbParamConstant;
import cc.rengu.igas.channel.esb.common.constant.EsbServiceConstant;
import cc.rengu.igas.channel.esb.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.esb.core.service.base.BankCoreAtomService;
import cc.rengu.igas.channel.esb.facade.request.CoreConsumeCancelResvalRequest;
import cc.rengu.igas.channel.esb.facade.response.CoreConsumeCancelResvalResponse;
import cc.rengu.jradp.schedule.ScheduleImpl;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.ConvertUtil;

/**
 * 核心消费撤销冲正服务
 * Created by wangcheng on 2021/9/3 10:34
 */
public class CoreConsumeCancelRevsalService extends BankCoreAtomService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        CoreConsumeCancelResvalRequest coreConsumeCancelResvalRequest = new CoreConsumeCancelResvalRequest();
        ConvertUtil.convertOutput(coreConsumeCancelResvalRequest);
        //TODO 请求参数合法性检查校验
        return coreConsumeCancelResvalRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        CoreConsumeCancelResvalRequest coreConsumeCancelResvalRequest = (CoreConsumeCancelResvalRequest) request;
        CoreConsumeCancelResvalResponse coreConsumeCancelResvalResponse = new CoreConsumeCancelResvalResponse();
        ConvertUtil.convertOutput(coreConsumeCancelResvalResponse);
        /* 演示环境发送短信默认返回成功 */
        if (AppParamConstant.DEFAULT_ENV.equals(AppParamConstant.APPLICATION_ENV)) {
            coreConsumeCancelResvalResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            coreConsumeCancelResvalResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
            //TODO 应答信息其它字段赋值
        } else {
            int iReturnCode = schedule.callService(EsbServiceConstant.BANK_CORE_CLIENT, EsbParamConstant.DEFAULT_SERVICE_LABEL);
            if (ScheduleImpl.SERVICE_SUCCESS == iReturnCode) {
                coreConsumeCancelResvalResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
                coreConsumeCancelResvalResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
                //TODO 应答信息其它字段赋值
            } else if (ScheduleImpl.SERVICE_TIMEOUT == iReturnCode) {
                rglog.error("callService {} 失败！返回值={}", EsbServiceConstant.BANK_CORE_CLIENT, iReturnCode);
                coreConsumeCancelResvalResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                coreConsumeCancelResvalResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            } else {
                rglog.error("callService {} 失败！返回值={}", EsbServiceConstant.BANK_CORE_CLIENT, iReturnCode);
                coreConsumeCancelResvalResponse.setRespCode(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
                coreConsumeCancelResvalResponse.setRespDesc(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            }
        }
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        chnlBizResponse.setResult(coreConsumeCancelResvalResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        CoreConsumeCancelResvalResponse coreConsumeCancelResvalResponse = (CoreConsumeCancelResvalResponse) response.getResult();
        ConvertUtil.convertInput(coreConsumeCancelResvalResponse);
    }
}
